import 'dart:io';

import 'package:flutter/services.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:jpush_flutter/jpush_interface.dart';
import 'package:tracker/common/log/x_logger.dart';
import 'package:tracker/common/permission/permission_manager.dart';
import 'package:tracker/startup/env/env.dart';
import '../../../../router/router_manager.dart';

class JPushUtils {

  static const String appKey = '26abc4010482269d5780f85b';
  static const String channel = 'theChannel';
  static bool isProduction = true;

  static final JPushFlutterInterface jpushM = JPush.newJPush();

  static initJPushSetup() {
  //
  }
  // /// 初始化极光推送
  static initJPush() async{
    XLogger.info("initJPush");
    // isProduction = !Env.isTest();
    isProduction = true;

    try {
       jpushM.addEventHandler(
          onOpenNotification: (Map<String, dynamic> message) async {
            XLogger.info("flutter onOpenNotification: $message");
            String? title = message['title'];
            if (title == '反馈回复') {
              RouterManager.routerConfig.push('${RouterManager.feedback}');
            }
            jpushM.clearAllNotifications();
          },
          onReceiveNotification: (Map<String, dynamic> message) async {
            XLogger.info("jpush onReceiveNotification: $message");
          },
          // 接收自定义消息回调方法。
          onReceiveMessage: (Map<String, dynamic> message) async {
            XLogger.info("jpush onReceiveMessage: $message");
          },
      );

    } on PlatformException {
      XLogger.info("'Failed to get platform version.");

    }
    XLogger.info("jpush 初始化");
    // 添加初始化方法，调用 setup 方法会执行两个操作：
    // 初始化 JPush SDK
    // 将缓存的事件下发到 dart 环境中。
    jpushM.setup(
      appKey: appKey,
      channel: channel,
      production: isProduction,
      debug: !isProduction,
    );

    // 请求 iOS 推送权限
    await requestAndroidPermission();
    await requestIOSPermission();
    getRegistrationID();
  }


  static getRegistrationID() async {
    String? rid = await jpushM.getRegistrationID();
    XLogger.info("RegistrationID: $rid");
    jpushM.setAuth(enable: true);
  }

    /// 设置别名
  static setAlias(String alias) async {
    XLogger.info('注册推送alias: $alias');
    if (alias.isEmpty) {
      XLogger.info('注册推送alias: $alias');
      return;
    }
    final oldAliasMap = await jpushM.getAlias();
    XLogger.info('oldAliasMap: $oldAliasMap');
    if (oldAliasMap.isNotEmpty && oldAliasMap['alias'] == alias) {
      XLogger.info('重复别名');
      return;
    }
    XLogger.info('注册推送alias: $alias');

    await jpushM.deleteAlias();
    try {
      await jpushM.setAlias(alias);
      XLogger.info('设置别名: $alias');

    } on PlatformException catch (e) {
      XLogger.info("极光返回错误: ${e.message}");
    } catch (e) {
      XLogger.info("未知错误: $e");
    }
  }
  //
  // /// 申请安卓通知权限
  static requestAndroidPermission() {
    jpushM.requestRequiredPermission();
  }
  //
  // /// 申请ios通知权限
  static requestIOSPermission() async {
    jpushM.applyPushAuthority(
      const NotificationSettingsIOS(sound: true, alert: true, badge: false),
    );
  }
  //
  static void deleteAlias() {
    jpushM.deleteAlias();
  }
}
